package com.cyh;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import reactor.core.publisher.Mono;

public class MonoDeferTest01 {

    public static void main(String[] args) throws InterruptedException {
        // 使用 defer() 创建一个延迟执行的 Mono
        Mono<String> timeMono = Mono.defer(() -> {
            // 每次订阅时都会执行这里的代码
            String currentTime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
            return Mono.just("Current time: " + currentTime);
        });

        System.out.println(timeMono);
        timeMono.subscribe(time -> System.out.println("First: " + time));
        Thread.sleep(2000);
        timeMono.subscribe(time -> System.out.println("Second: " + time));
    }
}
